package me.nereo.multi_image_selector.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.util.ArrayList;
import java.util.List;

import me.nereo.multi_image_selector.R;

//import com.squareup.picasso.Picasso;

/**
 * Created by donglua on 15/6/21.
 */
public class PhotoPagerAdapter extends PagerAdapter {

    private List<String> paths = new ArrayList<>();
    private List<String> small_paths = new ArrayList<>();
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public PhotoPagerAdapter(Context mContext, List<String> paths, List<String> small_paths) {
        this.mContext = mContext;
        this.paths = paths;
        this.small_paths = small_paths;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//4.5版本大图浏览
        View itemView = mLayoutInflater.inflate(R.layout.item_pager, container, false);

        final ImageView imageView = (ImageView) itemView.findViewById(R.id.iv_pager);
        final ImageView img_small = (ImageView) itemView.findViewById(R.id.img_small);
        final ProgressBar progressBar = (android.widget.ProgressBar) itemView.findViewById(R.id.ProgressBar);
//        final String path = /*"http://photo.l99.com/bigger/32/1371289116463_bv5ct5.jpg";//*/paths.get(position);
//        final Uri uri;
//        if (path.startsWith("http")) {
//            uri = Uri.parse(path);
//        } else {
//            uri = Uri.fromFile(new File(path));
//        }
        String bigpath = paths.get(position);
        String smallPath = null;
        if (small_paths != null && !small_paths.isEmpty()) {
            smallPath = small_paths.get(position);
            bigpath = TextUtils.isEmpty(bigpath) ? smallPath : bigpath;
        }

        /*修复加载 GIF 大图 3/28/2017 by leiyan*/
        if (bigpath.toLowerCase().endsWith(".gif")) {
            img_small.setVisibility(View.GONE);
            imageView.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .load(bigpath)
                    .asGif()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .error(R.drawable.ic_broken_image_black_48dp)
                    .into(imageView);
        } else {
            Glide.with(mContext)
                    .load(smallPath)
                    .into(img_small);/*列表中加载的小图，所以已经加载过了*/
            img_small.setVisibility(View.VISIBLE);
            imageView.setVisibility(View.GONE);
            Glide.with(mContext)
                    .load(bigpath)
                    .asBitmap()
                    .thumbnail(0.3f)
                    .error(R.drawable.ic_broken_image_black_48dp)
                    .placeholder(R.drawable.ic_photo_black_48dp)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                        /*便于加载 GIF 大图*/
                            progressBar.setVisibility(View.GONE);
                            img_small.setVisibility(View.GONE);
                            imageView.setVisibility(View.VISIBLE);
                            imageView.setImageBitmap(bitmap);
                        }

                        @Override
                        public void onLoadStarted(Drawable placeholder) {
                            super.onLoadStarted(placeholder);
                            progressBar.setVisibility(View.VISIBLE);
                        }

                    });
        }
        container.addView(itemView);

        return itemView;
    }


    @Override
    public int getCount() {
        return paths.size();
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}
